package com.living.player;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.living.Constant;
import com.living.player.db.AudioPlayListManger;
import com.living.player.db.RecentlyManger;
import com.living.player.event.EventMediaChange;
import com.living.player.event.EventPlayNewData;
import com.living.player.event.EventStatusPlay;
import com.living.player.services.MediaPlayerServiceImp;
import com.living.player.services.ServicePlayUtils;
import com.living.player.ui.player.VideoActivity;
import com.md.basedpc.persistence.PHSPUtil;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/***
 * 播放跳转的入口
 */
public class PlayUtils {

    /***
     * 专辑详情、搜索 入口
     * @param context
     * @param album
     * @param audio
     */
    public static void intentTo(Context context, Album album, Audio audio) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.putExtra("audio", audio);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        if (album != null) {
            // 加入播放列表
            AudioPlayListManger.getInstance().saveAudioList(album.getId(), album.getAudios());
            // 记录最近播放的数据
            RecentlyManger.getInstance().saveRecently(album, audio);
            Log.e("VideoActivity", " album: " + album.toString());
        }
        Log.e("VideoActivity", "  audio: " + audio.toString());
        PHSPUtil.getInstance().putString(Constant.player_recent_audio_id, audio.getId());
    }

    /***
     * 本地音频和最近播放 倒入播放列表
     * @param context
     * @param audio
     * @param audios
     */
    public static void addAudioListPlay(Context context, Audio audio, List<Audio> audios) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.putExtra("audio", audio);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        // 加入播放列表
        AudioPlayListManger.getInstance().saveAudioList(audio.getAlbumId(), audios);
        PHSPUtil.getInstance().putString(Constant.player_recent_audio_id, audio.getId());
    }

    public static void playAudio(Context context, Audio audio) {
        MediaPlayerServiceImp.playAudio(context, audio);
        // 加入播放列表
        PHSPUtil.getInstance().putString(Constant.player_recent_audio_id, audio.getId());
    }

    public static void PlayDailyAudio(Context context,List<Audio> audios, Audio todayAudio){
        // 加入播放列表
        AudioPlayListManger.getInstance().saveAudioList(todayAudio.getAlbumId(), audios);
        PHSPUtil.getInstance().putString(Constant.player_recent_audio_id, todayAudio.getId());
        MediaPlayerServiceImp.playAudio(context, todayAudio);
    }


}
